﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace Interface.Users
{
    public partial class UserDetails : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        StoreProcedures sp = new StoreProcedures();
        string stringDetail;
        protected override void OnInit(EventArgs e)
        {
            fview.ModeChanged += new EventHandler(fview_ModeChanged);
            
            
        }

        void fview_ModeChanged(object sender, EventArgs e)
        {
            
            DropDownList ddl = (DropDownList)fview.Row.FindControl("ddlGenderEdit");
            
           
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            getQueryString();
        }

        public void getQueryString()
        {
            if (Request.QueryString["ID"] != null)
            {
                stringDetail = Request.QueryString["ID"].ToString();
                DetailBind(stringDetail);
            }
        }

        protected void DetailBind(string userID)
        {
            string sqlStr = "Select * from Users where UserID='"+userID+"'";
            DataTable dt = dc.getData(sqlStr);
            DataTable dtUser = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["UserID"].ToString());
                    string userName = row["Username"].ToString();
                    string email = row["Email"].ToString();
                    string fullName = row["FullName"].ToString();                    
                    bool bgender = Boolean.Parse(row["Gender"].ToString());
                    string strGender = "";
                    if (bgender == true)
                        strGender = "Nam";
                    else
                        strGender = "Nữ";
                    string birthday = row["Birthday"].ToString();
                    string userType = row["MemberType"].ToString();
                    string phoneNo = row["PhoneNo"].ToString();
                    string address = row["Address"].ToString();
                    DataRow dtRow = dtUser.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["Username"] = userName;
                    dtRow["Email"] = email;
                    dtRow["FullName"] = fullName;
                    dtRow["Gender"] = strGender;
                    dtRow["Birthday"] = birthday;
                    dtRow["MemberType"] = userType;
                    dtRow["PhoneNo"] = phoneNo;
                    dtRow["Address"] = address;
                    dtUser.Rows.Add(dtRow);
                }
                fview.DataSource = dtUser;
                fview.DataBind();
            }
        }

        protected void ibtnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Users/EditUser.aspx?ID="+stringDetail);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(stringDetail);
            string fullName, email, gender, birthday, memberType, phoneNo, address;
            fullName = ((TextBox)fview.FindControl("txtNameEdit")).Text;
            email = ((TextBox)fview.FindControl("txtEmailEdit")).Text;
            gender = ((DropDownList)fview.FindControl("ddlGenderEdit")).SelectedValue.ToString();
            int intMale = 0;
            if (gender == "Nam")
            {
                intMale = 1;
            }
            //birthday = ((Calendar)fview.FindControl("cldEdit")).SelectedDate.ToLongDateString();
            birthday = ((TextBox)fview.FindControl("txtEditDOB")).Text;
            memberType = ((DropDownList)fview.FindControl("ddlMBTEdit")).SelectedValue.ToString();
            phoneNo = ((TextBox)fview.FindControl("txtPhoneNoEdit")).Text;            
            address = ((TextBox)fview.FindControl("txtAddEdit")).Text;

            sp.UpdateUser(id, email, fullName, intMale, birthday, memberType, phoneNo, address);

            lblMs.Text = "Cập nhật thông tin thành công";
            fview.ChangeMode(FormViewMode.ReadOnly);       
        }

        protected void cldEdit_Changed(object sender, EventArgs e)
        {
            TextBox txtBD = (TextBox)fview.FindControl("txtEditDOB");
            txtBD.Text = ((Calendar)fview.FindControl("cldEdit")).SelectedDate.ToShortDateString(); 
        }

        protected void ibtnBack_Edit(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx");
        }

        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;           
            dt.Columns.Add("Username", typeof(string));
            dt.Columns.Add("FullName", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("Gender", typeof(string));
            dt.Columns.Add("Birthday", typeof(string));
            dt.Columns.Add("MemberType", typeof(string));
            dt.Columns.Add("PhoneNo", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            return dt;
        }
        

    }
}